#include "Application.h"
#include "Graphics.h"
#include "Game.h"

static Application* gApp = NULL;

static int gResolution = 512;

Application::Application(void)
{
	mWindow = NULL;
	mGraphics = NULL;
	mGame = NULL;
}

Application::~Application(void)
{
}

Application* Application::GetApplication()
{
	if (!gApp) {
		gApp = new Application();
	}

	return gApp;
}

bool Application::Init()
{
	mWindow = new sf::Window(sf::VideoMode(gResolution, gResolution, 32), "Checkers", sf::Style::Close);

	mGraphics = Graphics::GetGraphics();
	mGraphics->Init(mWindow);

	mGame = Game::GetGame();
	mGame->Init();

	return true;
}

bool Application::Run()
{
	while (mWindow->IsOpened())
	{			
		sf::Event event;
		while (mWindow->GetEvent(event))
		{
			if (event.Type == sf::Event::Closed)
			{
				mWindow->Close();
			}
			else 
			{
				mGame->ProcessEvent(event);
			}
		}

		bool result = mGame->Update(mWindow->GetFrameTime());

		if (!result) 
		{
			mWindow->Close();
		}

		mWindow->SetActive();
		mGraphics->Render();

		mWindow->Display();
	}

	return true;
}


